var tools = {};

function isPointInTriangle(pointP, pointsTri){
    var PA = pointsTri[0].sub(pointP);
    var PB = pointsTri[1].sub(pointP);
    var PC = pointsTri[2].sub(pointP);
    var t1 = PA.cross(PB)
    var t2 = PB.cross(PC)
    var t3 = PC.cross(PA)
    return t1*t2 > 0 && t1*t3 > 0;
}

function checkCollisionWithTri(playernode, trinode){
    var pa = playernode.getScreenPos();
    //var palast = playernode.getLastScreenPos();
    var ptris = trinode.getScreenPos();
    var collision = isPointInTriangle(pa, ptris);
    //var collisionlast = isPointInTriangle(palast, ptris);
    return collision //&& !collisionlast;
}

tools.checkCollisionWithTri = checkCollisionWithTri;
module.exports = tools;